package com.link.base.base.serviceprojectnew.controller;

import com.link.base.base.product.model.PriceListItem;
import com.link.base.base.serviceprojectnew.model.Project;
import com.link.base.base.serviceprojectnew.model.ProjectTeam;
import com.link.base.base.serviceprojectnew.service.ProjectTeamService;
import com.link.core.cllog.LCLogger;
import com.link.core.basic.annotations.JsonParam;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.model.BasicModel;
import com.link.core.basic.query.QueryParams;
import com.link.core.basic.service.BasicService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author unknown
 * @date unknown
 * @comments 项目成员
 */
@Controller
@RequestMapping("/link/projectTeam")
public class ProjectTeamController extends BasicController<ProjectTeam> {
    @Resource
    private ProjectTeamService projectTeamService;

    @Override
    public BasicService<ProjectTeam> getBasicService() throws Exception {
        return projectTeamService;
    }

    @RequestMapping(value = "/queryUnSelectMemberInProjectManagerPage")
    @ResponseBody
    public Map<String, Object> queryUnSelectMemberInProjectManagerPage(@JsonParam QueryParams qps, @JsonParam Project entity, HttpSession session,
                                                                       HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            qps.preDealSecurity(request);
            Project t = (Project) BasicModel.transformClass(entity.getClass(), qps);
            t.setCorpid(Long.parseLong(request.getParameter("corpid")));
            List<ProjectTeam> list = ((ProjectTeamService) getBasicService()).queryUnSelectMemberInProjectManagerPage(t);
            result.put("success", true);
            result.put("rows", list);
            this.pushCurrentDataCache(list, session.getId(), "base", false);
            result.put("total", getCount((BasicModel) t, list.size()));
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("rows", new ArrayList<PriceListItem>());
            result.put("result", e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "/multiAddProjectTeam", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> multiAddProjectTeam(@RequestBody List<ProjectTeam> projectTeams, QueryParams qps,
                                                   HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            for (int i = 0; projectTeams != null && i < projectTeams.size(); i++) {
                projectTeamService.insert(projectTeams.get(i));
            }
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }
}
